<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button size="small" type="primary" @click="editRow(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRow(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->

              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.page"
                :page-sizes="[10, 20, 30]"
                :page-size="page.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <el-dialog :title="dialogTitle" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          show-checkbox
          check-strictly
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        ></el-tree>
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  getRoleDetail,
  updateRole,
  addRole,
  assignPerm,
} from '@/api/setting';
import { getPermissionList } from '@/api/permission';
import { tranListToTreeData } from '@/utils';
import { mapGetters } from 'vuex';
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'first',
      list: [], // 待会放数据的
      page: {
        page: 1, // 当前第几页
        pagesize: 10, // 每页放多少条数据
        total: 0, // 总条数
      },
      formData: {},
      showDialog: false,
      roleForm: {},
      showPermDialog: false, // 分配权限的弹出框
      permData: [], // 放tree展示数据的
      defaultProps: {
        label: 'name',
      },
      selectCheck: [],
      roleId: '',
    };
  },
  created() {
    this.getRoleList();
    this.getCompanyInfo();
  },
  mounted() {},
  methods: {
    async getRoleList() {
      // 请求数据
      const res = await getRoleList({ page: this.page.page, pagesize: this.page.pagesize });
      // 拿到数据就开始赋值
      this.list = res.data.data.rows;
      this.page.total = res.data.data.total;
    },
    async getCompanyInfo() {
      // const res = await getCompanyInfo(this.$store.state.user.userInfo.companyId);
      // const res = await getCompanyInfo(this.$store.getters.companyId);
      const res = await getCompanyInfo(this.companyId);
      this.formData = res.data.data;
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    handleCurrentChange(page) {
      this.page.page = page;
      this.getRoleList();
    },
    handleSizeChange(newSize) {
      this.page.pagesize = newSize;
      this.getRoleList();
    },
    // 删除角色
    deleteRow(id) {
      deleteRole(id).then((res) => {
        console.log('res:', res);
        this.getRoleList();
      });
    },
    // 编辑角色
    async editRow(id) {
      this.showDialog = true;
      const res = await getRoleDetail(id);
      console.log('res:', res.data.data);
      this.roleForm = res.data.data;
    },
    // 取消编辑弹窗
    btnCancel() {
      this.roleForm = {};
      this.showDialog = false;
    },
    // 点击确定 提交更新数据
    btnOk() {
      if (this.roleForm.id) {
        updateRole(this.roleForm).then((res) => {
          this.btnCancel();
          this.getRoleList();
        });
      } else {
        addRole(this.roleForm).then((res) => {
          this.btnCancel();
          this.getRoleList();
        });
      }
    },
    async assignPerm(id) {
      this.roleId = id;
      this.showPermDialog = true;
      const res = await getPermissionList();
      this.permData = tranListToTreeData(res.data.data, '0');
      const roleDetail = await getRoleDetail(id);
      this.selectCheck = roleDetail.data.data.permIds;
    },
    btnPermCancel() {
      this.selectCheck = [];
      this.showPermDialog = false;
    },
    async btnPermOK() {
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() });
      this.showPermDialog = false;
    },
  },
  computed: {
    ...mapGetters(['companyId']),
    dialogTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色';
    },
  },
};
</script>

<style lang="scss"></style>
